<?php

namespace backend\models\form;

use backend\models\AdminUser;
use common\models\Feed;
use common\service\ConstatService;
use Yii;
use yii\base\Model;
use yii\helpers\FileHelper;

class FeedForm extends Model
{
    public $desc;
    public $contact;
    public $company_name;
    public $deal_user_id;
    public $contact_name;
    public $pic_url1;
    public $pic_url2;
    public $pic_url3;
    public $video_url;
    public $address;
    public $remark ;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['desc','contact','company_name'], 'required'],
            [['deal_user_id'], 'integer'],
            [['desc', 'contact','company_name','contact_name','address'], 'string', 'max' => 255],
            [['pic_url1', 'pic_url2','pic_url3','video_url','remark'], 'string', 'max' => 255],
            [['pic_url1','pic_url2','pic_url3'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png,jpg,gif,jpeg'],
            [['video_url'], 'file', 'skipOnEmpty' => true],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'desc' => '描述 ',
            'contact' => '联系方式',
            'company_name' => '公司名称',
            'deal_user_id' => '处理人',
            'contact_name' => '联系人' ,
            'pic_url1'  => '问题图片1',
            'pic_url2'  => '问题图片2',
            'pic_url3'  => '问题图片3',
            'video_url' => '视频',
            'address'   => '地址' ,
            'remark'    => '备注' ,
        ];
    }

    /**
     * 新增
     * @return array
     */
    public function add(){
        $msg = "新增";
        $model = new Feed();

        if(empty($this->deal_user_id)){
            $dealUserId = 0 ;
            $dealUserName =  "" ;
        }else{
            $user = AdminUser::findOne(["user_id"=>$this->deal_user_id]);
            if(empty($user)){
                return ["code"=>400,"msg"=>"处理人不存在"];
            }
            $dealUserId = $this->deal_user_id ;
            $dealUserName =  $user['username'] ;
        }

        $curTime = date("Y-m-d H:i:s") ;

        $model->desc = $this->desc ;
        $model->contact = $this->contact ;
        $model->company_name = $this->company_name;
        $model->address = $this->address ;
        $model->deal_user_id = $dealUserId;
        $model->deal_user_name = $dealUserName ;
        $model->contact_name = $this->contact_name ;
        if(empty($this->deal_user_id)){
            $model->static = ConstatService::FEED_STATIC_NOT_DEAL ;
        }else{
            $model->static = ConstatService::FEED_STATIC_DEALING ;
        }
        $model->remark = $this->remark ;

        $type_id = Yii::$app->admin->identity->type_id ;
        if($type_id == ConstatService::ADMIN_USER_TYPE_ID_MANAGER || $type_id == ConstatService::ADMIN_USER_TYPE_ID_SUPER_ADMIN){
            //管理员添加是已审核
            $model->verify = ConstatService::FEED_VERIFY_VERIFYED ;
        }else{
            //普通员工添加是未审核
            $model->verify = ConstatService::FEED_VERIFY_NOT_VERIFY ;
        }

        $picUrl1 = "" ;
        $picUrl2 = "" ;
        $picUrl3 = "" ;
        $videoUrl = "" ;

        $dir = date("Ymd") ;
        $webRoot = Yii::getAlias("@webroot") ;
        $basePath = "/uploads/{$dir}/" ;
        FileHelper::createDirectory($webRoot.$basePath) ;
        $hostName = Yii::$app->request->hostInfo;

        if(!empty($this->pic_url1)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url1->extension;
            $picUrl1  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url1->saveAs($savePath);
        }

        if(!empty($this->pic_url2)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url2->extension;
            $picUrl2  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url2->saveAs($savePath);
        }

        if(!empty($this->pic_url3)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url3->extension;
            $picUrl3  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url3->saveAs($savePath);
        }

        if(!empty($this->video_url)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->video_url->extension;
            $videoUrl  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->video_url->saveAs($savePath);
        }


        $model->pic_url1 = $picUrl1 ;
        $model->pic_url2 = $picUrl2 ;
        $model->pic_url3 = $picUrl3 ;
        $model->video_url = $videoUrl ;
        $model->add_time = $curTime ;
        $model->upd_time = $curTime ;

        $save = $model->save();
        if($save){
            return ["code"=>200,"msg"=>"成功"];
        }else{
            $err = current($model->getFirstErrors()) ;
            return ["code"=>400,"msg"=>"添加失败-".$err];
        }
    }


    /**
     * 修改
     * @return array
     */
    public function edit($feed_id){
        $model = Feed::findOne(["id"=>$feed_id]);
        if(empty($model)){
            return ["code"=>400,"msg"=>"工单不存在"];
        }

        if(empty($this->deal_user_id)){
            $dealUserId = 0 ;
            $dealUserName =  "" ;
        }else{
            $user = AdminUser::findOne(["user_id"=>$this->deal_user_id]);
            if(empty($user)){
                return ["code"=>400,"msg"=>"处理人不存在"];
            }
            $dealUserId = $this->deal_user_id ;
            $dealUserName =  $user['username'] ;
        }




        $curTime = date("Y-m-d H:i:s") ;

        $model->desc = $this->desc ;
        $model->contact = $this->contact ;
        $model->company_name = $this->company_name;
        $model->address = $this->address ;
        $model->deal_user_id = $dealUserId;
        $model->deal_user_name = $dealUserName ;
        $model->contact_name = $this->contact_name ;
        $model->remark = $this->remark ;

        $picUrl1 = "" ;
        $picUrl2 = "" ;
        $picUrl3 = "" ;
        $videoUrl = "" ;

        $dir = date("Ymd") ;
        $webRoot = Yii::getAlias("@webroot") ;
        $basePath = "/uploads/{$dir}/" ;
        FileHelper::createDirectory($webRoot.$basePath) ;
        $hostName = Yii::$app->request->hostInfo;

        if(!empty($this->pic_url1)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url1->extension;
            $picUrl1  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url1->saveAs($savePath);
        }

        if(!empty($this->pic_url2)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url2->extension;
            $picUrl2  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url2->saveAs($savePath);
        }

        if(!empty($this->pic_url3)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->pic_url3->extension;
            $picUrl3  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->pic_url3->saveAs($savePath);
        }

        if(!empty($this->video_url)){
            $filename = date("YmdHis") . rand(10000, 99999) . '.' . $this->video_url->extension;
            $videoUrl  = $hostName . $basePath . $filename;
            $savePath = $webRoot . $basePath . $filename;
            $this->video_url->saveAs($savePath);
        }


        if(!empty($picUrl1)){
            $model->pic_url1 = $picUrl1 ;
        }

        if(!empty($picUrl2)){
            $model->pic_url2 = $picUrl2 ;
        }

        if(!empty($picUrl3)){
            $model->pic_url3 = $picUrl3 ;
        }

        if(!empty($videoUrl)){
            $model->video_url = $videoUrl ;
        }

        $model->upd_time = $curTime ;

        if($model->static==ConstatService::FEED_STATIC_NOT_DEAL){
            if(!empty($this->deal_user_id)){
                $model->static = ConstatService::FEED_STATIC_DEALING ;
            }
        }

        $type_id = Yii::$app->admin->identity->type_id ;
        if($model->verify==ConstatService::FEED_VERIFY_NOT_VERIFY){
            if($type_id == ConstatService::ADMIN_USER_TYPE_ID_MANAGER || $type_id == ConstatService::ADMIN_USER_TYPE_ID_SUPER_ADMIN){
                //管理员修改变成已审核
                $model->verify = ConstatService::FEED_VERIFY_VERIFYED ;
            }
        }

        $save = $model->save();
        if($save){
            return ["code"=>200,"msg"=>"修改成功"];
        }else{
            $err = current($model->getFirstErrors()) ;
            return ["code"=>400,"msg"=>"修改失败-".$err];
        }
    }
}
